/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.articles.importFromLn.articlesData.pesta;

/**
 *
 * @author x211800
 */
public class ColumnOfPages extends Columns {

    private int countOfScalePages;
    private StringBuffer note;
    private static ColumnOfPages instance = null;
    private String parsedPages;

    private ColumnOfPages() {
    }

    public static ColumnOfPages instanceOf(String columnContent) throws ColumnsError{
        if (instance == null){
            instance = new ColumnOfPages();
        }
        instance.parseColumn(columnContent);
        return instance;
    }


/**
 *
 * @param column[7]
 * @return String{parsed pages}
 * @throws ColumnsError{see message}
 */
    public void parseColumn(String column) throws ColumnsError {
        String pages = null;
        countOfScalePages = 0;
        note = new StringBuffer("");

        /*
         * parsing pages
         */
        if (!stringToInteger(column)) {
            pages = parsePages(removeTextDelimits(column));
        } else {
            pages = String.valueOf(column);
        }


        countOfScalePages = pages.split(COMA).length;

        if (countOfScalePages == 0) {
            System.err.println("problem with scale pages: " + pages);
        }

        parsedPages = pages;
    }


    private String parsePages(String unparsedPages) throws ColumnsError {
        String toParse = removeDiacritics(unparsedPages);
        if (containsFotoObr(toParse)) {
            if (toParse.contains(COMA)) {
                return analyzePages(toParse.split(COMA));
            } else if (toParse.contains(String.valueOf(WHITESPACE))) {
                throw new ColumnsError("problem with analyzing obr and foto");
            }
        } else {
            return removeWhiteSpaces(unparsedPages);
        }
        return null;
    }

    private boolean containsFotoObr(String toAnalyze) {
        return (toAnalyze.contains("obr") || toAnalyze.contains("fot") || toAnalyze.contains("kreb") || toAnalyze.contains("fak"));
    }

    private String analyzePages(String[] toAnalyze) {
        StringBuffer bufferedPages = new StringBuffer("");
        for (int i = 0; i < toAnalyze.length; i++) {
            if (containsFotoObr(toAnalyze[i])) {
                if (note.length() != 0) {
                    note.append(COMA + WHITESPACE);
                }
                note.append(toAnalyze[i]);
            } else {
                bufferedPages.append(toAnalyze[i]);
                countOfScalePages++;
            }
        }
        return bufferedPages.toString();
    }

    /**
     *
     * @return int{count of scale pages}
     */
    public int getCountOfScalePages() {
        return countOfScalePages;
    }

    /**
     *
     * @return StringBuffer{addition to note - information of pictures, photos, ...}
     */
    public StringBuffer getNote() {
        return note;
    }

    public String getParsedPages() {
        return parsedPages;
    }



}
